<?php
/* This notice must be untouched at all times.

inputprocessing.php    v. 2.2
This file is part of PhyloGeoViz
The latest version is available at
http://www.phylogeoviz.com

Copyright (c) 2007 Yi-Hsin Erica Tsai. All rights reserved.
Created 15.7.2007 by Yi-Hsin Erica Tsai (Web: http://www.duke.edu/~yet2)
Last modified: 20.8.2007

PhyloGeoViz is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

PhyloGeoViz is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function pv ($var)
{
	echo "<pre>";
	var_dump($var);  //var_dump is a php function that outputs array data and other objects in a readable way
	echo "</pre>";
}

$is_error = false; //when encounters an error, will set this variable to true
$message = "";

//////  process phylogeography data from input field 1 //////

$exploded_phylodata = explode("\n", trim($_REQUEST["phylodata"]));  //trim is there to remove surrounding whitespace that otherwise causes viewer to choke.

//fix input data whitespace issues
for ($i = 0; $i < count($exploded_phylodata); $i++)
{
	while(strpos($exploded_phylodata[$i], "  ")) 
	{
		$exploded_phylodata[$i] = str_replace("  ", " ", $exploded_phylodata[$i]); 
	}
	$exploded_phylodata[$i] = str_replace(" ", "\t", $exploded_phylodata[$i]);  //convert all spaces to tabs for proper processing
	$exploded_phylodata[$i] = explode("\t", $exploded_phylodata[$i]);
}

$numpops = count($exploded_phylodata);
$numhaps = count($exploded_phylodata[0]) - 3;
$pop_names = array();
$pop_lat = array();
$pop_lng = array();
$pop_haps = array();
$pop_include = array();

for ($p = 0; $p < $numpops; $p++)
{
	$pop_names[$p] = trim($exploded_phylodata[$p][0]);
	$pop_lat[$p] = trim($exploded_phylodata[$p][1]);
	$pop_lng[$p] = trim($exploded_phylodata[$p][2]);
	$pop_include[$p] = 1;  //all pops are included at beginning
	
	for ($h = 0; $h < $numhaps; $h++)
		$pop_haps[$p][$h] = trim($exploded_phylodata[$p][$h + 3]);
}

//////  process haplotype data from input field 2 //////

$hap_names = array();
$hapgroups = array();
$hap_include = array();

//if the user fills out the optional haplotype information field
if ($_REQUEST["hapgroupdata"] != "")
{
	$exploded_hapgroupdata = explode("\n", trim($_REQUEST["hapgroupdata"]));  

	if (count($exploded_hapgroupdata) != $numhaps)  //check that the data in the two fields agree with one another.
	{
		$message = "Field 1 and field 2 data disagree!  Please <a href=\"index.html\"> resubmit.</a>";
		
		$is_error = true;
	}
		
	else  //input data is the right number of rows, so process
	{
		for ($h = 0; $h < $numhaps; $h++)
		{
			while(strpos($exploded_hapgroupdata[$h], "  ")) 
			{
				$exploded_hapgroupdata[$h] = str_replace("  ", " ", $exploded_hapgroupdata[$h]); 
			}
			$exploded_hapgroupdata[$h] = str_replace(" ", "\t", $exploded_hapgroupdata[$h]);  //convert all spaces to tabs for proper processing
			$exploded_hapgroupdata[$h] = explode("\t", $exploded_hapgroupdata[$h]);
			
			$hap_names[$h] = trim($exploded_hapgroupdata[$h][0]);
			$hapgroups[$h] = trim($exploded_hapgroupdata[$h][1]);
			$hap_include[$h] = 1;  //all haplotypes are included at the beginning
		}
	}
}

else  //optional data not set by user, so we just set default names and groups
{
	for ($h = 0; $h < $numhaps; $h++)
	{
		$hap_names[$h] = "hap".$h;
		$hapgroups[$h] = "hapgroup".$h;
		$hap_include[$h] = 1;  //all haplotypes are included at the beginning
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
		<script type="text/javascript" src="javascripts/prototype.js"></script>
		<title>Processing your pie chart data</title>
		<link type="text/css" rel="stylesheet" href="phylogeoviz.css" />
		<link type="text/css" rel="stylesheet" href="previewstyle.css" />
	</head>

<?php
	if ($is_error)
	{
?>
	<body>
		<h1> Error! </h1>
<?php 
		echo $message;
?>
		<p><a href="index.html">Return to the input data screen.</a></p>
	</body>
<?php
	}
	
	else
	{
?>
	<body class="loadinggif" onload="$('drawmap').submit()">
		<div id="page">
			<div id="loadingflower">
				<h2>Loading your map...</h2>
				<p>Please be patient, this may take a minute...</p>
				<img src="images/ajax-loader-green.gif" style="border: none">

				<form id="drawmap" name="drawmap" method="post" action="newviewer.php">
					<input type="hidden" id="numpops" name="numpops" value="<?php echo $numpops ?>">
					<input type="hidden" id="numhaps" name="numhaps" value="<?php echo $numhaps ?>">
					<input type="hidden" id="pop_names" name="pop_names" value="<?php echo htmlspecialchars(json_encode($pop_names)) ?>">
					<input type="hidden" id="hap_names" name="hap_names" value="<?php echo htmlspecialchars(json_encode($hap_names)) ?>">		
					<input type="hidden" id="pop_lat" name="pop_lat" value="<?php echo htmlspecialchars(json_encode($pop_lat)) ?>">
					<input type="hidden" id="pop_lng" name="pop_lng" value="<?php echo htmlspecialchars(json_encode($pop_lng)) ?>">
					<input type="hidden" id="pop_haps" name="pop_haps" value="<?php echo htmlspecialchars(json_encode($pop_haps)) ?>">
					<input type="hidden" id="hapgroups" name="hapgroups" value="<?php echo htmlspecialchars(json_encode($hapgroups)) ?>">
					<input type="hidden" id="pop_include" name="pop_include" value="<?php echo htmlspecialchars(json_encode($pop_include)) ?>">
					<input type="hidden" id="hap_include" name="hap_include" value="<?php echo htmlspecialchars(json_encode($hap_include)) ?>">
					<input type="submit" class="hidden" value="Draw Map!">
				</form>
			</div>
		</div>
	</body>
<?php
	}
?>
</html>
